package mvc.controller;

import com.google.gson.Gson;
import mvc.model.pojos.UserBean;
import mvc.model.service.UserService;
import mvc.model.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpUserNameServlet", urlPatterns = "/userName")

public class UpUserNameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        UserBean user = (UserBean) request.getSession().getAttribute("user");
        UserService service = new UserServiceImpl();
        user.setUsername(username);
        boolean b = service.updateUser(user.getUid(), user);
        user = service.queryOne(user.getUid());
        request.getSession().setAttribute("user", user);
        response.getWriter().append(new Gson().toJson(b));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
